package org.taotao.manager.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.taotao.commons.vo.TreeNodeVo;
import org.taotao.manager.pojo.ItemCat;
import org.taotao.manager.service.ItemCatService;

@Controller
@RequestMapping("/cat")
public class ItemCatController {
	@Autowired
	private ItemCatService itemCatService;
	
	@RequestMapping("/list")
	@ResponseBody
	public List<TreeNodeVo> getItemCat(@RequestParam(defaultValue="0", value="id")int id){
		List<TreeNodeVo> treeNodeVos = new ArrayList<>();
		List<ItemCat> itemCats = itemCatService.getItemCatByParentId(id);
		if (!CollectionUtils.isEmpty(itemCats)){
			for (ItemCat itemCat: itemCats){
				TreeNodeVo treeNodeVo = new TreeNodeVo();
				treeNodeVo.setId(itemCat.getId());
				treeNodeVo.setText(itemCat.getName());
				treeNodeVo.setState(itemCat.getIsParent() ? "closed": "open");
				treeNodeVos.add(treeNodeVo);
			}
		}
		return treeNodeVos;
		
	}
}
